home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / TownMaze.lha / TownMaze / src.lzh / makestreet.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  9KB  |  289 lines

  1. /*
  2. ** makestreet.c  Copyright 1991 Kent Paul Dolan,
  3. **               Mountain View, CA, USA 94039-0755
  4. **
  5. ** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
  6. ** May be freely used or modified in any non-commercial work.  Copyrighted
  7. ** only to prevent patenting by someone else.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "townmaze.h"
  12. #include "townproto.h"
  13.  
  14. #ifdef __STDC__
  15. int makestreet(int chosencell,int streetid,int streetpoints)
  16. #else
  17. int makestreet(chosencell,streetid,streetpoints)
  18.   int chosencell,streetid, streetpoints;
  19. #endif
  20. {
  21.  
  22.   int choseni, chosenj;
  23.   int nhbrid, nextnhbrid, thirdnhbrid;
  24.   int streetwalk, highstreetnum, lowstreetnum;
  25.   int canstraighten;
  26.  
  27. /*
  28. ** Check that a street isn't going in next to an unused cell; if it is,
  29. ** mark it dead, instead if it's live; in any case, refuse to do it.
  30. */
  31.  
  32.   for (nhbrid = 0; nhbrid < 4; nhbrid++)
  33.   {
  34.     if (nhbrexists(chosencell,nhbrid))
  35.       if (statlist[nhbris(chosencell,nhbrid)].status == UNUSED)
  36.       {
  37.         if (statlist[chosencell].status == LIVE)
  38.           movefromto(&live,&livect,&dead,&deadct,DEAD,chosencell);
  39.         return(1==0);
  40.       }
  41.   }
  42.    
  43. /*
  44. ** Check for and abide by straightness control
  45. */
  46.  
  47.    if ((mazestraightness > 0) && (streetpoints == -1))
  48.      if ((RANDOM()%1000) < mazestraightness)
  49.      {
  50.        canstraighten = (1==0);
  51.        for (nhbrid = 0; nhbrid < 4; nhbrid++)
  52.          if (nhbrexists(chosencell,nhbrid))
  53.            if (statlist[nhbris(chosencell,nhbrid)].streetdir ==
  54.                ((nhbrid+2)%4))
  55.              canstraighten = (1==1);
  56.        if (!canstraighten) return(1==0);
  57.      }
  58. /*
  59. ** Update the chosen cell in the status list
  60. */
  61.   switch (statlist[chosencell].status)
  62.   {
  63.   case UNUSED:
  64.   case STREET:
  65.     fprintf(stderr,"error picking street; bad cell status %d\n",
  66.       statlist[chosencell].status);
  67.     freespace();
  68.     exit(1);
  69.     break;
  70.  
  71.   case LIVE:
  72. /*  fprintf(stderr,"moving cell %d from live to street\n",chosencell); */
  73.     movefromto(&live,&livect,&street,&streetct,STREET,chosencell);
  74.     break;
  75.  
  76.   case DEAD:
  77. /*  fprintf(stderr,"moving cell %d from dead to street\n",chosencell); */
  78.     movefromto(&dead,&deadct,&street,&streetct,STREET,chosencell);
  79.     break;
  80.  
  81.   case ISOLATED:
  82. /*  fprintf(stderr,"moving cell %d from isolated to street\n",chosencell);*/
  83.     movefromto(&isolated,&isolatedct,&street,&streetct,STREET,chosencell);
  84.     break;
  85.  
  86.   default:
  87.     fprintf(stderr,"unexpected chosencell status in makegates %d\n",
  88.       statlist[chosencell].status);
  89.   }
  90.  
  91. /*
  92. ** Give the street the required streetid, either new or continuing an
  93. ** existing street.
  94. */
  95.  
  96.   statlist[chosencell].streetnum = streetid;
  97.  
  98. /*
  99. ** Update the chosen cell on the map.
  100. */
  101.  
  102.   choseni = chosencell / (mazewidth/2);
  103.   chosenj = chosencell % (mazewidth/2);
  104.  
  105.   choseni = 2 * choseni + 1;
  106.   chosenj = 2 * chosenj + 1;
  107.  
  108.   cmaze[choseni - 1][chosenj] = HDOOR;
  109.   cmaze[choseni][chosenj + 1] = VDOOR;
  110.   cmaze[choseni + 1][chosenj] = HDOOR;
  111.   cmaze[choseni][chosenj - 1] = VDOOR;
  112.  
  113. /*
  114. ** Update the neighbors of the new street cell;
  115. */
  116.  
  117.   for (nhbrid = 0; nhbrid < 4; nhbrid++)
  118.   {
  119.     if (nhbrexists(chosencell,nhbrid))
  120.     switch (statlist[nhbris(chosencell,nhbrid)].status)
  121.     {
  122.     case UNUSED:
  123.       fprintf(stderr,
  124.               "logic error; tried to put a street beside an unused cell\n");
  125.       showdebugmaze();
  126.       freespace();
  127.       exit(1);
  128.       break;
  129.     case STREET:
  130.       switch (nhbrid) /* change the door to a passage, adopt a continuing */
  131.       {               /* direction from a neighbor street */
  132.       case 0:
  133.         cmaze[choseni - 1][chosenj] = BLANK;
  134.         if (statlist[nhbris(chosencell,nhbrid)].streetdir == 2)
  135.           statlist[chosencell].streetdir = 2;
  136.         break;
  137.       case 1:
  138.         cmaze[choseni][chosenj + 1] = BLANK;
  139.         if (statlist[nhbris(chosencell,nhbrid)].streetdir == 3)
  140.           statlist[chosencell].streetdir = 3;
  141.         break;
  142.       case 2:
  143.         cmaze[choseni + 1][chosenj] = BLANK;
  144.         if (statlist[nhbris(chosencell,nhbrid)].streetdir == 0)
  145.           statlist[chosencell].streetdir = 0;
  146.         break;
  147.       case 3:
  148.         cmaze[choseni][chosenj - 1] = BLANK;
  149.         if (statlist[nhbris(chosencell,nhbrid)].streetdir == 1)
  150.           statlist[chosencell].streetdir = 1;
  151.         break;
  152.       default:
  153.         fprintf(stderr,"bad nhbrid in nhbr update street switch case\n");
  154.         /* The above should be impossible; nhbrexists and nhbris already */
  155.         /* both checked for this problem */
  156.         showdebugmaze();
  157.         freespace();
  158.         exit(1);
  159.       }
  160.  
  161.       if (statlist[chosencell].streetnum !=
  162.           statlist[nhbris(chosencell,nhbrid)].streetnum)
  163.       {
  164.  
  165. /*
  166. ** Two different streets have met; merge their streetids and decrement the
  167. ** streetcount.
  168. */
  169.  
  170.         lowstreetnum =
  171.           ( ( statlist[chosencell].streetnum >
  172.               statlist[nhbris(chosencell,nhbrid)].streetnum )
  173.             ? statlist[nhbris(chosencell,nhbrid)].streetnum 
  174.             : statlist[chosencell].streetnum
  175.           );
  176.         highstreetnum =
  177.           ( ( statlist[chosencell].streetnum <
  178.               statlist[nhbris(chosencell,nhbrid)].streetnum )
  179.             ? statlist[nhbris(chosencell,nhbrid)].streetnum 
  180.             : statlist[chosencell].streetnum
  181.           );
  182.  
  183.         streetwalk = street;
  184.         while (streetwalk != NOPOINTER)
  185.         {
  186.           if (statlist[streetwalk].streetnum == highstreetnum)
  187.             statlist[streetwalk].streetnum = lowstreetnum;
  188.           streetwalk = statlist[streetwalk].next;
  189.         }
  190.         streetnumct--;
  191.       }
  192.       break;
  193.     case DEAD: /* do nothing; that's why it's called dead */
  194.       break;
  195.     case LIVE:
  196.       movefromto(&live,&livect,&dead,&deadct,DEAD,nhbris(chosencell,nhbrid));
  197.       for (nextnhbrid = 0; nextnhbrid < 4; nextnhbrid++)
  198.         if (nhbrexists(nhbris(chosencell,nhbrid),nextnhbrid))
  199.           if (statlist[nhbris(nhbris(chosencell,nhbrid),nextnhbrid)].status
  200.               == ISOLATED)
  201.           {
  202.             movefromto(&dead,&deadct,&live,&livect,LIVE,
  203.                        nhbris(chosencell,nhbrid));
  204.             break;
  205.           }
  206.       break;
  207.     case ISOLATED:
  208.       movefromto(&isolated,&isolatedct,&dead,&deadct,DEAD,
  209.                  nhbris(chosencell,nhbrid));
  210.       statlist[nhbris(chosencell,nhbrid)].status = DEAD;
  211. /*
  212. ** Only let an isolated cell become live if it is interior; stops
  213. ** streets from running along the city wall, creating lots of gates.
  214. ** Don't make it live if it is next to an unused cell, either, since
  215. ** it can never become a street cell.
  216. */
  217.       if (   interiorcell(nhbris(chosencell,nhbrid))
  218.           && (statlist[nhbris(nhbris(chosencell,nhbrid),0)].status != UNUSED)
  219.           && (statlist[nhbris(nhbris(chosencell,nhbrid),1)].status != UNUSED)
  220.           && (statlist[nhbris(nhbris(chosencell,nhbrid),2)].status != UNUSED)
  221.           && (statlist[nhbris(nhbris(chosencell,nhbrid),3)].status != UNUSED)
  222.          )
  223.         for (nextnhbrid = 0; nextnhbrid < 4; nextnhbrid++)
  224.           if (nhbrexists(nhbris(chosencell,nhbrid),nextnhbrid))
  225.             if (statlist[nhbris(nhbris(chosencell,nhbrid),nextnhbrid)].status
  226.                 == ISOLATED)
  227.             {
  228.               movefromto(&dead,&deadct,&live,&livect,LIVE,
  229.                          nhbris(chosencell,nhbrid));
  230.               break;
  231.             }
  232. /*
  233. ** When an isolated cell stops being so, neighbors may stop being live
  234. ** that were depending on it for that status; we're not done!
  235. */
  236.       for (nextnhbrid = 0; nextnhbrid < 4; nextnhbrid++)
  237.         if (nhbrexists(nhbris(chosencell,nhbrid),nextnhbrid))
  238.           if (statlist[nhbris(nhbris(chosencell,nhbrid),nextnhbrid)].status
  239.               == LIVE)
  240.           {
  241.             movefromto(&live,&livect,&dead,&deadct,DEAD,
  242.                        nhbris(nhbris(chosencell,nhbrid),nextnhbrid));
  243.             for (thirdnhbrid =0; thirdnhbrid <4; thirdnhbrid++)
  244.             {
  245.               if (nhbrexists(nhbris(nhbris(chosencell,
  246.                                     nhbrid),
  247.                              nextnhbrid),
  248.                   thirdnhbrid))
  249.                 if (statlist[nhbris(nhbris(nhbris(chosencell,
  250.                                            nhbrid),
  251.                                     nextnhbrid),
  252.                              thirdnhbrid)].status
  253.                     == ISOLATED)
  254.                 {
  255.                   movefromto(&dead,&deadct,&live,&livect,LIVE,
  256.                              nhbris(nhbris(chosencell,nhbrid),nextnhbrid));
  257.                   break;
  258.                 }
  259.             }
  260.           }
  261.       break;
  262.     default:
  263.       fprintf(stderr,"bad statlist[%d].status entry %d\n",
  264.               nhbris(chosencell,nhbrid),
  265.               statlist[nhbris(chosencell,nhbrid)].status);
  266.       showdebugmaze();
  267.       freespace();
  268.       exit(1);
  269.     }
  270.   }
  271.   if (statlist[chosencell].streetdir == -1)
  272.      if (streetpoints != -1)
  273.        statlist[chosencell].streetdir = streetpoints;
  274.      else
  275.      {
  276.        for (nhbrid = 0; nhbrid < 4; nhbrid++)
  277.        {
  278.          if (nhbrexists(chosencell,nhbrid))
  279.            if (statlist[nhbris(chosencell,nhbrid)].status == STREET)
  280.            {
  281.              statlist[chosencell].streetdir = ((nhbrid +2)%4);
  282.              break;
  283.            }
  284.        }
  285.      }
  286. /*  showdebugmaze(); */
  287.   return(1==1);
  288. }
  289.